﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using OnlineManagement.Model;

namespace OnlineManagement.Web.WebServices
{
    /// <summary>
    /// WeanedRecordService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    public class WeanedRecordService : System.Web.Services.WebService
    {

        [WebMethod]
        public JsonMsg WeanedRecordAdd
            (int hoggeryid, Guid sowId, Guid hybrizationRecordID,
            Guid propagateRecordID, DateTime weanedDate, int weanedChildCount,int totalChildCount,
            int isChangeSowPigPen, Guid changeToPigPenID,string comment)
        {

             JsonMsg msg = new JsonMsg();
            if(weanedChildCount<=0){
                msg.Flag = false;
                msg.Message = "断奶母猪数量不能小于零";
                return msg;
            }
            //断奶母猪数量不能大于乳猪总数量
             if(weanedChildCount>totalChildCount){
                 msg.Flag = false;
                 msg.Message = "断奶母猪数量不能大于乳猪总数量";
                 return msg;
             }
            if(isChangeSowPigPen!=0&&isChangeSowPigPen!=1){
                msg.Flag = false;
                msg.Message = "您输入的信息有误，请检查后重新提交";
                return msg;
            }
            //0--母猪转栏，1--乳猪转栏
            bool isChangedPigPen = isChangeSowPigPen == 0 ? true : false;
            double survivalRate = weanedChildCount*1.0 / totalChildCount*1.0;
            string survivalRateStr = survivalRate.ToString("P");
            log4net.ILog log = OnlineHoggeryLog.GetLog("Web.WebServices.WeanedRecordService.WeanedRecordAdd");

            Model.WeanedRecord record = new WeanedRecord()
            {
                HoggeryID = hoggeryid,
                SowID = sowId,
                HybrizationRecord = hybrizationRecordID,
                PropagateRecordID = propagateRecordID,
                WeanedDate = weanedDate,
                WeanedChildCount = weanedChildCount,
                Comment = comment,
                SurvivalRate = survivalRateStr
            };
            try
            {
                string returnValue;
                BLL.WeanedRecord bllWR = new BLL.WeanedRecord();
                bool result = bllWR.AddWeanedRecord(record, isChangedPigPen, changeToPigPenID, out returnValue);
                msg.Flag = result;
                msg.Message = returnValue;

              
                Com.HoggeryLog bussinessLog = new Com.HoggeryLog() { 
                        HoggeryID=hoggeryid,
                        PigId=sowId.ToString(), 
                        BusinessLogDate=weanedDate, 
                        LogType=Com.HoggeryLogType.WeanedRecord
                };
                bussinessLog.Message = returnValue;
                log.Info(bussinessLog);
                return msg;
            }catch(Exception ex){
                log.Error(new Com.HoggeryLog() {HoggeryID=hoggeryid }, ex);
                msg.Flag = false;
                msg.Message = Common.PubMsg.ServerError;
                return msg;
            }
        }
    }
}
